package cn.szzsi.platform.controller.web;

import cn.szzsi.AConfig;
import cn.szzsi.business.service.FileService;
import cn.szzsi.common.bean.dto.FileDirectory;
import cn.szzsi.common.util.CommonUtil;
import cn.szzsi.core.file.FileMsg;
import com.jfinal.core.Controller;
import com.jfinal.core.JFinal;
import com.jfinal.kit.JsonKit;
import com.jfinal.kit.PropKit;
import com.jfinal.kit.StrKit;
import com.jfinal.upload.UploadFile;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.StringUtils;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 后台Action类 - 文件上传
 * 
 */
public class UploadController extends Controller {


	private File upload;// 上传文件
	private String uploadFileName;// 上传文件名
	
	// 图片文件上传
	public void upload() {
		getResponse().setHeader("Access-Control-Allow-Origin", "*");
		UploadFile imgageFile = getFile();
		if (imgageFile != null) {
			upload = imgageFile.getFile();
			uploadFileName = imgageFile.getFileName();
		}
		String shopId = getPara("shopId");
		String shopFilePath = FileService.fs.getShopFileRoot(shopId);
		String imageExtension =  StringUtils.substringAfterLast(uploadFileName, ".").toLowerCase();
		File uploadImageDir = new File(JFinal.me().getServletContext().getRealPath(shopFilePath));
		if (!uploadImageDir.exists()) {
			uploadImageDir.mkdirs();
		}
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMM");
		String dateString = simpleDateFormat.format(new Date());
		String uploadImagePath = shopFilePath + dateString + "/" + CommonUtil.getUUID() + "." + imageExtension;
		File file = new File(JFinal.me().getServletContext().getRealPath(uploadImagePath));
		try {
			FileUtils.copyFile(upload, file);
			FileUtils.deleteQuietly(upload);
		} catch (IOException e) {
			e.printStackTrace();
		}
		Map<String, Object> jsonMap = new HashMap<String, Object>();

		jsonMap.put("message", new FileMsg());
		jsonMap.put("url", AConfig.p.get("file.server") + JFinal.me().getServletContext().getContextPath() + uploadImagePath);
		renderHtml(JsonKit.toJson(jsonMap));
	}
	public void browser() {
		getResponse().setHeader("Access-Control-Allow-Origin", "*");
		getResponse().setHeader("Content-Type","application/json;charset=UTF-8");
		String orderType =  getPara("orderType");
		String sid = getPara("sid");
		String shopFilePath = FileService.fs.getShopFileRoot(sid);

		String filePath = JFinal.me().getServletContext().getRealPath(shopFilePath);
		String path = getPara("path");
		if(StrKit.notBlank(path))
			filePath = filePath + File.separator + org.apache.commons.lang3.StringUtils.remove(path,"/");
		File uploadImageDir = new File(filePath);
		if (!uploadImageDir.exists()) {
			uploadImageDir.mkdirs();
		}

		List<FileDirectory> listFileD = new ArrayList<FileDirectory>();
		if (uploadImageDir.exists()) {
			File[] files = uploadImageDir.listFiles();
			FileService.fs.sortFiles(files,orderType);
			for(File f:files){
				FileDirectory fd = new FileDirectory();
				if (f.isDirectory()) {
					fd.setIsDirectory(true);
				} else {
					fd.setIsDirectory(false);
				}
				fd.setLastModified(f.lastModified());
				fd.setName(f.getName());
				fd.setSize(f.getFreeSpace());
				fd.setUrl(AConfig.p.get("file.server") + shopFilePath+path+f.getName());
				listFileD.add(fd);
			}
		}

		renderJson(listFileD);
	}


}
